package com.limh.keepalive.core

import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager

/**
 * @author limh
 * @function 键盘相关
 * @date 2021/4/20 15:22
 */
interface KeyboardAction {
    /**
     * 显示软键盘
     */
    fun showKeyboard(view: View?) {
        if (view == null) {
            return
        }
        val manager = view.context
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        manager?.showSoftInput(view, 0)
    }

    /**
     * 隐藏软键盘
     */
    fun hideKeyboard(view: View?) {
        if (view == null) {
            return
        }
        val manager = view.context
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        manager?.hideSoftInputFromWindow(
            view.windowToken,
            InputMethodManager.HIDE_NOT_ALWAYS
        )
    }

    /**
     * 切换软键盘
     */
    fun toggleSoftInput(view: View?) {
        if (view == null) {
            return
        }
        val manager = view.context
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        manager?.toggleSoftInput(0, 0)
    }
}